package com.yh.blog.controller.admin;
import com.yh.blog.domain.Friend;
import com.yh.blog.domain.Picture;
import com.yh.blog.service.FriendService;
import com.yh.blog.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 相册管理控制层
 */
@Controller
@RequestMapping("/admin")
public class PictureController {
    @Autowired
    private PictureService pictureService;

//    相册列表
    @GetMapping("/picture")
    public String index(Model model, @RequestParam(value = "page",defaultValue = "1") Integer page){
        model.addAttribute("pager",this.pictureService.list(page));
        return "admin/picture/pictures";
    }

//  跳转到新增图片页面
    @GetMapping("/picture/input")
    public String input(){
        return "admin/picture/pictures-append";
    }
//    新增图片
    @PostMapping("/picture")
    public String append(Picture picture, RedirectAttributes attributes){
        if(this.pictureService.append(picture)){
            attributes.addFlashAttribute("messages","新增成功");
        }else {
            attributes.addFlashAttribute("messages","新增失败");
        }
        return "redirect:/admin/picture";
    }

//    跳转到编辑图片页面
    @GetMapping("/picture/modify")
    public String editInput(@RequestParam("pictureId") Integer pictureId, Model model) {
        model.addAttribute("picture",this.pictureService.detail(pictureId));
        return "admin/picture/pictures-modify";
    }
//    编辑图片
    @PostMapping("/picture/modify")
    public String editPost(Picture picture,RedirectAttributes attributes) {
        if(this.pictureService.modify(picture)){
            attributes.addFlashAttribute("messages","修改成功");
        }else {
            attributes.addFlashAttribute("messages","修改失败");
        }
        return "redirect:/admin/picture";
    }


//  删除图片
    @RequestMapping("/picture/delete")
    public String delete(Integer pictureId, RedirectAttributes attributes){
        this.pictureService.remove(pictureId);
        attributes.addFlashAttribute("messages","删除成功");
        return "redirect:/admin/picture";
    }
}
